package com.kingbull.webservice;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

import com.kingbull.web.communic.Body;
import com.kingbull.web.communic.Head;
import com.kingbull.web.communic.Subject;
import com.kingbull.web.utils.ResponseCode;
import net.sf.json.JSONObject;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class CxfClient {
	public static void main(String[] args) {
		cl1();
		System.out.println("###########################");
		cl2();
//		System.out.println("###########################");
//		cl3();
	}

	/**
	 * 方式1.代理类工厂的方式,需要拿到对方的接口
	 */
	public static void cl1() {
		try {
			// 接口地址
			String address = "http://localhost:8080/services/CommonService?wsdl";
			// 代理工厂
			JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
			// 设置代理地址
			jaxWsProxyFactoryBean.setAddress(address);
			// 设置接口类型
			jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
			// 创建一个代理接口实现
			CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
			// 数据准备
			Subject sub = new Subject(
					new Head("1003","0102","AJSHDUWJKSMNSHHDJNSJSKDJJS321232","2020-01-01 23:59:59",ResponseCode.SUCCESS_CODE,"",""),
					new Body("230421199009090911","12324EREW"));
			JSONObject object = JSONObject.fromObject(sub);
			String jsonstr = object.toString();
			System.out.println("发送消息:"+jsonstr);
			String backobj= cs.Entrance(jsonstr);
			System.out.println("返回结果:" + backobj);
//			JSONObject jsonobject = JSONObject.fromObject(backobj);
//			// 调用代理接口的方法调用并返回结果
//			System.out.println(jsonobject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 动态调用方式
	 */
	public static void cl2() {
		// 创建动态客户端
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		Client client = dcf.createClient("http://localhost:8080/services/CommonService?wsdl");
		// 需要密码的情况需要加上用户名和密码
		// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,
		// PASS_WORD));
		Object[] objects = new Object[0];
		try {
			// invoke("方法名",参数1,参数2,参数3....);
			Subject sub = new Subject(
					new Head("1003","0102","AJSHDUWJKSMNSHHDJNSJSKDJJS321232","2020-01-01 23:59:59",ResponseCode.SUCCESS_CODE,"",""),
					new Body("230421199009090911","12324EREW"));
			JSONObject object = JSONObject.fromObject(sub);
			String jsonstr = object.toString();
			objects = client.invoke("IDCard", jsonstr);
			System.out.println("返回数据:" + objects[0]);
		} catch (java.lang.Exception e) {
			e.printStackTrace();
		}
	}

	public static void cl3() {

		Service service = Service.create(SERVICE_NAME);
		// Endpoint Address
		String endpointAddress = "http://localhost:8080/services/CommonService";
		// Add a port to the Service
		service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);

		CommonService cs = service.getPort(CommonService.class);

		System.out.println(cs.sayHello("world"));

	}

	private static final QName SERVICE_NAME = new QName("http://webservice.kingbull.com/", "CommonService");
	private static final QName PORT_NAME = new QName("http://webservice.kingbull.com/", "CommonServicePort");
}
